/*
 * Use to decode received respond from server.
 */
package org.dbdir.client;

import java.sql.Timestamp;
import java.util.ArrayList;
import org.dbdir.core.helper.ACL;
import org.dbdir.core.helper.Database;

/**
 *
 * @author thotheolh
 */
public class RespondHandler {

    public RespondHandler() {
    }

    public ACL getACL(String response) {
        String rslist[] = response.split(" ");
        String[] value1 = rslist[1].split("=");
        String[] value2 = rslist[2].split("=");
        String[] value3 = rslist[3].split("=");
        String[] value4 = rslist[4].split("=");
        String[] value5 = rslist[5].split("=");
        String[] value6 = rslist[6].split("=");
        String[] value7 = rslist[7].split("=");
        String[] value8 = rslist[8].split("=");
        String[] value9 = rslist[9].split("=");
        String key_Username = "usr";
        String key_Alias = "dbalias";
        String key_showDbPath = "sdbpath";
        String key_showDbUsername = "sdbusr";
        String key_showDbPassword = "sdbpwd";
        String key_showDateTimeReg = "sdtreg";
        String key_editDbPath = "edbpath";
        String key_editDbUsername = "edbusr";
        String key_editDbPassword = "edbpwd";
        String val_Username = null;
        String val_Alias = null;
        boolean val_showDbPath = false;
        boolean val_showDbUsername = false;
        boolean val_showDbPassword = false;
        boolean val_showDateTimeReg = false;
        boolean val_editDbPath = false;
        boolean val_editDbUsername = false;
        boolean val_editDbPassword = false;
        if (rslist[0].equals("getaclrs")) {
            if (value1[0].equals(key_Username)) {
                val_Username = value1[1];
            } else if (value1[0].equals(key_Alias)) {
                val_Alias = value1[1];
            } else if (value1[0].equals(key_showDbPath)) {
                val_showDbPath = convertToBool(value1[1]);
            } else if (value1[0].equals(key_showDbUsername)) {
                val_showDbUsername = convertToBool(value1[1]);
            } else if (value1[0].equals(key_showDbPassword)) {
                val_showDbPassword = convertToBool(value1[1]);
            } else if (value1[0].equals(key_showDateTimeReg)) {
                val_showDateTimeReg = convertToBool(value1[1]);
            } else if (value1[0].equals(key_editDbPath)) {
                val_editDbPath = convertToBool(value1[1]);
            } else if (value1[0].equals(key_editDbUsername)) {
                val_editDbUsername = convertToBool(value1[1]);
            } else if (value1[0].equals(key_editDbPassword)) {
                val_editDbPassword = convertToBool(value1[1]);
            }
            if (value2[0].equals(key_Username)) {
                val_Username = value2[1];
            } else if (value2[0].equals(key_Alias)) {
                val_Alias = value2[1];
            } else if (value2[0].equals(key_showDbPath)) {
                val_showDbPath = convertToBool(value2[1]);
            } else if (value2[0].equals(key_showDbUsername)) {
                val_showDbUsername = convertToBool(value2[1]);
            } else if (value2[0].equals(key_showDbPassword)) {
                val_showDbPassword = convertToBool(value2[1]);
            } else if (value2[0].equals(key_showDateTimeReg)) {
                val_showDateTimeReg = convertToBool(value2[1]);
            } else if (value2[0].equals(key_editDbPath)) {
                val_editDbPath = convertToBool(value2[1]);
            } else if (value2[0].equals(key_editDbUsername)) {
                val_editDbUsername = convertToBool(value2[1]);
            } else if (value2[0].equals(key_editDbPassword)) {
                val_editDbPassword = convertToBool(value2[1]);
            }
            if (value3[0].equals(key_Username)) {
                val_Username = value3[1];
            } else if (value3[0].equals(key_Alias)) {
                val_Alias = value3[1];
            } else if (value3[0].equals(key_showDbPath)) {
                val_showDbPath = convertToBool(value3[1]);
            } else if (value3[0].equals(key_showDbUsername)) {
                val_showDbUsername = convertToBool(value3[1]);
            } else if (value3[0].equals(key_showDbPassword)) {
                val_showDbPassword = convertToBool(value3[1]);
            } else if (value3[0].equals(key_showDateTimeReg)) {
                val_showDateTimeReg = convertToBool(value3[1]);
            } else if (value3[0].equals(key_editDbPath)) {
                val_editDbPath = convertToBool(value3[1]);
            } else if (value3[0].equals(key_editDbUsername)) {
                val_editDbUsername = convertToBool(value3[1]);
            } else if (value3[0].equals(key_editDbPassword)) {
                val_editDbPassword = convertToBool(value3[1]);
            }
            if (value4[0].equals(key_Username)) {
                val_Username = value4[1];
            } else if (value4[0].equals(key_Alias)) {
                val_Alias = value4[1];
            } else if (value4[0].equals(key_showDbPath)) {
                val_showDbPath = convertToBool(value4[1]);
            } else if (value4[0].equals(key_showDbUsername)) {
                val_showDbUsername = convertToBool(value4[1]);
            } else if (value4[0].equals(key_showDbPassword)) {
                val_showDbPassword = convertToBool(value4[1]);
            } else if (value4[0].equals(key_showDateTimeReg)) {
                val_showDateTimeReg = convertToBool(value4[1]);
            } else if (value4[0].equals(key_editDbPath)) {
                val_editDbPath = convertToBool(value4[1]);
            } else if (value4[0].equals(key_editDbUsername)) {
                val_editDbUsername = convertToBool(value4[1]);
            } else if (value4[0].equals(key_editDbPassword)) {
                val_editDbPassword = convertToBool(value4[1]);
            }
            if (value5[0].equals(key_Username)) {
                val_Username = value5[1];
            } else if (value5[0].equals(key_Alias)) {
                val_Alias = value5[1];
            } else if (value5[0].equals(key_showDbPath)) {
                val_showDbPath = convertToBool(value5[1]);
            } else if (value5[0].equals(key_showDbUsername)) {
                val_showDbUsername = convertToBool(value5[1]);
            } else if (value5[0].equals(key_showDbPassword)) {
                val_showDbPassword = convertToBool(value5[1]);
            } else if (value5[0].equals(key_showDateTimeReg)) {
                val_showDateTimeReg = convertToBool(value5[1]);
            } else if (value5[0].equals(key_editDbPath)) {
                val_editDbPath = convertToBool(value5[1]);
            } else if (value5[0].equals(key_editDbUsername)) {
                val_editDbUsername = convertToBool(value5[1]);
            } else if (value5[0].equals(key_editDbPassword)) {
                val_editDbPassword = convertToBool(value5[1]);
            }
            if (value6[0].equals(key_Username)) {
                val_Username = value6[1];
            } else if (value6[0].equals(key_Alias)) {
                val_Alias = value6[1];
            } else if (value6[0].equals(key_showDbPath)) {
                val_showDbPath = convertToBool(value6[1]);
            } else if (value6[0].equals(key_showDbUsername)) {
                val_showDbUsername = convertToBool(value6[1]);
            } else if (value6[0].equals(key_showDbPassword)) {
                val_showDbPassword = convertToBool(value6[1]);
            } else if (value6[0].equals(key_showDateTimeReg)) {
                val_showDateTimeReg = convertToBool(value6[1]);
            } else if (value6[0].equals(key_editDbPath)) {
                val_editDbPath = convertToBool(value6[1]);
            } else if (value6[0].equals(key_editDbUsername)) {
                val_editDbUsername = convertToBool(value6[1]);
            } else if (value6[0].equals(key_editDbPassword)) {
                val_editDbPassword = convertToBool(value6[1]);
            }
            if (value7[0].equals(key_Username)) {
                val_Username = value7[1];
            } else if (value7[0].equals(key_Alias)) {
                val_Alias = value7[1];
            } else if (value7[0].equals(key_showDbPath)) {
                val_showDbPath = convertToBool(value7[1]);
            } else if (value7[0].equals(key_showDbUsername)) {
                val_showDbUsername = convertToBool(value7[1]);
            } else if (value7[0].equals(key_showDbPassword)) {
                val_showDbPassword = convertToBool(value7[1]);
            } else if (value7[0].equals(key_showDateTimeReg)) {
                val_showDateTimeReg = convertToBool(value7[1]);
            } else if (value7[0].equals(key_editDbPath)) {
                val_editDbPath = convertToBool(value7[1]);
            } else if (value7[0].equals(key_editDbUsername)) {
                val_editDbUsername = convertToBool(value7[1]);
            } else if (value7[0].equals(key_editDbPassword)) {
                val_editDbPassword = convertToBool(value7[1]);
            }
            if (value8[0].equals(key_Username)) {
                val_Username = value8[1];
            } else if (value8[0].equals(key_Alias)) {
                val_Alias = value8[1];
            } else if (value8[0].equals(key_showDbPath)) {
                val_showDbPath = convertToBool(value8[1]);
            } else if (value8[0].equals(key_showDbUsername)) {
                val_showDbUsername = convertToBool(value8[1]);
            } else if (value8[0].equals(key_showDbPassword)) {
                val_showDbPassword = convertToBool(value8[1]);
            } else if (value8[0].equals(key_showDateTimeReg)) {
                val_showDateTimeReg = convertToBool(value8[1]);
            } else if (value8[0].equals(key_editDbPath)) {
                val_editDbPath = convertToBool(value8[1]);
            } else if (value8[0].equals(key_editDbUsername)) {
                val_editDbUsername = convertToBool(value8[1]);
            } else if (value8[0].equals(key_editDbPassword)) {
                val_editDbPassword = convertToBool(value8[1]);
            }
            if (value9[0].equals(key_Username)) {
                val_Username = value9[1];
            } else if (value9[0].equals(key_Alias)) {
                val_Alias = value9[1];
            } else if (value9[0].equals(key_showDbPath)) {
                val_showDbPath = convertToBool(value9[1]);
            } else if (value9[0].equals(key_showDbUsername)) {
                val_showDbUsername = convertToBool(value9[1]);
            } else if (value9[0].equals(key_showDbPassword)) {
                val_showDbPassword = convertToBool(value9[1]);
            } else if (value9[0].equals(key_showDateTimeReg)) {
                val_showDateTimeReg = convertToBool(value9[1]);
            } else if (value9[0].equals(key_editDbPath)) {
                val_editDbPath = convertToBool(value9[1]);
            } else if (value9[0].equals(key_editDbUsername)) {
                val_editDbUsername = convertToBool(value9[1]);
            } else if (value9[0].equals(key_editDbPassword)) {
                val_editDbPassword = convertToBool(value9[1]);
            }

            ACL acl = new ACL(val_Username, val_Alias, val_showDbPath, val_showDbUsername, val_showDbPassword, val_showDateTimeReg, val_editDbPath, val_editDbUsername, val_editDbPassword);
            return acl;
        } else {
            return null;
        }

    }

    public Database getDB(String response) {//handle the value of getdbrs
        String rslist[] = response.split(" ");
        String[] value1 = rslist[1].split("=");
        String[] value2 = rslist[2].split("=");
        String[] value3 = rslist[3].split("=");
        String[] value4 = rslist[4].split("=");
        String[] value5 = rslist[5].split("=");
        String key_dbalias = "dbalias";
        String key_dbpath = "dbpath";
        String key_dbusr = "dbusr";
        String key_dbpwd = "dbpwd";
        String key_dtreg = "datetime";
        String val_dbalias = null;
        String val_dbpath = null;
        String val_dbusr = null;
        String val_dbpwd = null;
        String val_dtreg = null;
        if (rslist[0].equals("getdbrs")) {
            if (value1[0].equals(key_dbalias)) {
                val_dbalias = value1[1];
            } else if (value1[0].equals(key_dbpath)) {
                val_dbpath = value1[1];
            } else if (value1[0].equals(key_dbusr)) {
                val_dbusr = value1[1];
            } else if (value1[0].equals(key_dbpwd)) {
                val_dbpwd = value1[1];
            } else if (value1[0].equals(key_dtreg)) {
                val_dtreg = value1[1];
            }
            if (value2[0].equals(key_dbalias)) {
                val_dbalias = value2[1];
            } else if (value2[0].equals(key_dbpath)) {
                val_dbpath = value2[1];
            } else if (value2[0].equals(key_dbusr)) {
                val_dbusr = value2[1];
            } else if (value2[0].equals(key_dbpwd)) {
                val_dbpwd = value2[1];
            } else if (value2[0].equals(key_dtreg)) {
                val_dtreg = value2[1];
            }
            if (value3[0].equals(key_dbalias)) {
                val_dbalias = value3[1];
            } else if (value3[0].equals(key_dbpath)) {
                val_dbpath = value3[1];
            } else if (value3[0].equals(key_dbusr)) {
                val_dbusr = value3[1];
            } else if (value3[0].equals(key_dbpwd)) {
                val_dbpwd = value3[1];
            } else if (value3[0].equals(key_dtreg)) {
                val_dtreg = value3[1];
            }
            if (value4[0].equals(key_dbalias)) {
                val_dbalias = value4[1];
            } else if (value4[0].equals(key_dbpath)) {
                val_dbpath = value4[1];
            } else if (value4[0].equals(key_dbusr)) {
                val_dbusr = value4[1];
            } else if (value4[0].equals(key_dbpwd)) {
                val_dbpwd = value4[1];
            } else if (value4[0].equals(key_dtreg)) {
                val_dtreg = value4[1];
            }
            if (value5[0].equals(key_dbalias)) {
                val_dbalias = value5[1];
            } else if (value5[0].equals(key_dbpath)) {
                val_dbpath = value5[1];
            } else if (value5[0].equals(key_dbusr)) {
                val_dbusr = value5[1];
            } else if (value5[0].equals(key_dbpwd)) {
                val_dbpwd = value5[1];
            } else if (value5[0].equals(key_dtreg)) {
                val_dtreg = value5[1];
            }
            Timestamp time = null;
            if (val_dtreg.equals("null")) {
            } else {
                String[] charArray = val_dtreg.split("&-");
                String value = charArray[0] + " " + charArray[1];
                time = Timestamp.valueOf(value);
            }
            Database db = new Database(val_dbalias, val_dbpath, val_dbusr, val_dbpwd, time);
            return db;
        } else {
            return null;
        }
    }

    public ArrayList convertStringToArray(String respond, String expHead) {//expHead is the expected respond header like 'getdblsrs' before the data
        ArrayList rs = new ArrayList();
        String resplist[] = respond.split(" ");
        if (resplist[0].equals(expHead)) {
            try {
                String str = resplist[1];
                String list[] = str.split(",");
                for (int i = 0; i < list.length; i++) {
                    String rslist[] = list[i].split("\"");
                    rs.add(rslist[1].toString());
                }
                return rs;
            } catch (ArrayIndexOutOfBoundsException ex) {
                return null;
            }
        } else {
            return null;
        }
    }

    public String convertBoolToStr(boolean bool) {
        if (bool == true) {
            return "true";
        } else {
            return "false";
        }
    }

    public boolean convertToBool(String string) {
        if (string.equalsIgnoreCase("true")) {
            return true;
        } else {
            return false;
        }
    }
}
